﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Configuration;
using Ext.Net;
using System.Data;
using Newtonsoft.Json;
using System.IO;
using System.Text;


namespace TCDC_System.Content.MasterData
{
    public partial class p_tcdc_mst_function : SystemPage.UIPage
    {
        private tcdc_mst_function objFunction;
        protected void Page_Init(object sender, EventArgs e)
        {
            this.FunctionAdd1.GridStore = StoreFunction;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            objFunction = new tcdc_mst_function();
            objFunction.strConn = ConfigurationManager.AppSettings["ConnString"];
            if (!X.IsAjaxRequest)
            {
                StoreFunction.DataSource = GetFunction();
                StoreFunction.DataBind();
            }
        }

        protected DataTable GetFunction()
        {
            DataTable dt = objFunction.getByObject(new tcdc_mst_function());
            return dt;
        }

        protected void AddFunction(object sender, DirectEventArgs e)
        {
            FunctionAdd1.Show();
        }

        protected void UpdateFunction(object sender, DirectEventArgs e)
        {
            string json = e.ExtraParams["FunctionValue"];
            Dictionary<string, string>[] workingdays = JSON.Deserialize<Dictionary<string, string>[]>(json);
            string function_id = string.Empty;
            string function_name = string.Empty;
            string parent_id = string.Empty;
            string sorting_no = string.Empty;
            string url = string.Empty;
            string create_date = string.Empty;
            string create_by = string.Empty;
            string update_date = string.Empty;
            string update_by = string.Empty;

            foreach (Dictionary<string, string> row in workingdays)
            {
                function_id = row["function_id"].ToString();
                function_name = row["function_name"].ToString();
                parent_id = row["parent_id"].ToString();
                sorting_no = row["sorting_no"].ToString();
                url = row["url"].ToString();
                create_date = row["create_date"].ToString();
                create_by = row["create_by"].ToString();
                update_date = row["update_date"].ToString();
                update_by = row["update_by"].ToString();
            }

            objFunction.function_id = Convert.ToInt32(function_id);
            objFunction.function_name = function_name;
            objFunction.parent_id = Convert.ToInt32(parent_id);
            objFunction.sorting_no = Convert.ToInt32(sorting_no);
            objFunction.url = url;
            objFunction.create_date = create_date;
            objFunction.create_by = Convert.ToInt32(create_by);
            objFunction.update_date = update_date;
            objFunction.update_by = Convert.ToInt32(update_by);
            objFunction.updateData(objFunction);
            StoreFunction.DataSource = GetFunction();
            StoreFunction.DataBind();
        }

        protected void DeleteFunction(object sender, DirectEventArgs e)
        {
            this.ConfirmDeleteFunction();
        }

        protected void StoreFunction_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            StoreFunction.DataSource = GetFunction();
            StoreFunction.DataBind();
        }

        protected void ExportExcelFunction(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();

            DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));

            if (dt.Rows.Count > 0)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=Function.xls");
                Response.ContentType = "application/vnd.ms-excel";
                StringWriter stringWriter = new StringWriter();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
                DataGrid dataExportExcel = new DataGrid();
                dataExportExcel.DataSource = dt;
                dataExportExcel.DataBind();
                dataExportExcel.RenderControl(htmlWrite);
                StringBuilder sbResponseString = new StringBuilder();
                sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head> <body>");
                sbResponseString.Append(stringWriter + "</body></html>");
                sbResponseString.Replace("&nbsp;", "&#160;");
                Response.Write(sbResponseString.ToString());
                Response.End();
            }
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void ConfirmDeleteFunction()
        {
            X.Msg.Confirm("Confirm", "ยืนยันการลบ ?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = "MyNameSpace.DeleteDataFunction()",
                    Text = "Yes"
                },
                No = new MessageBoxButtonConfig { Text = "No" }
            }).Show();
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void DeleteDataFunction()
        {
            RowSelectionModel sm = this.GridPanelFunction.SelectionModel.Primary as RowSelectionModel;
            string function_id = string.Empty;

            foreach (SelectedRow row in sm.SelectedRows)
            {
                function_id = row.RecordID;
            }
            objFunction.deleteData(function_id);
            StoreFunction.DataSource = GetFunction();
            StoreFunction.DataBind();
        }
    }
}

